$(function(){
	// 获取个人资料
	getUserInfo({
		success: function(res){
				if(res.code == 0){
				var data = res.data;
				$(".user-accoutn").text(data.phone).attr;
				$(".user-number").text(data.code);
				$(".user-name").text(data.username);
				$(".super-user").text(data.is_superuser? '是' : '否');
				if(data.is_superuser){
					$("#pro_invitecode").removeClass("hide");
				}
			}else{
				alert(res.msg)
			}
		},
		fail: function(res){
				var res = JSON.parse(res.responseText)
				alert(res.msg)
		}
	})
	// 点击修改用户信息
	$("#edit_userinfo").on("click", function(e){
	  e.stopPropagation();
	  var name = $(".user-name").eq(0).text();
	  $("#user_name").val(name);
	  $("#editModal").modal("show");
	})
	$("#editModal .modal-sure").on("click", function(e){
	  e.stopPropagation();
	  var code = $(".user-number").eq(0).text();
	  var name = $("#user_name").val().trim();
	  $.ajax({
	  	url: "/api/auth/user_info/"+code+"/",
	  	data: {
			username: name
	  	},
	  	dataType: "JSON",
	  	type: "PUT",
	  	success: function(res){
	  		if(res.code == 0){
				var data = res.data;
				$(".user-accoutn").text(data.phone);
				$(".user-number").text(data.code);
				$(".user-name").text(data.username);
				$(".super-user").text(data.is_superuser? '是' : '否');
				if(data.is_superuser){
					$("#pro_invitecode").removeClass("hide");
				}
			    $("#editModal").modal("hide");
			    $("#editModal input").val("");
			}else{
				alert(res.msg)
			}
	  	},
	  	error: function(res){
	  		var res = JSON.parse(res.responseText)
			alert(res.msg)
	  	}
	  })
	})
	
	// 点击修改密码
	$("#change_password").on("click", function(e){
	  e.stopPropagation();
	  $("#passwordModal").modal("show");
	})

	$("#passwordModal .modal-sure").on("click", function(e){
	  e.stopPropagation();
	  var old_password = $("#password").val().trim();
	  var new_password = $("#new_password").val().trim();
	  var sure_new_password = $("#sure_new_password").val().trim();
	  if(old_password === ""){
	  	alert("原密码不能为空");
	  }else if(new_password === ""){
	  	alert("新密码不能为空")
	  }else if(new_password !== sure_new_password){
	  	alert("前后两次输入的新密码不一致");
	  }else{
	  	$.post("/api/auth/change_password/",{
		  	old_password: old_password,
		  	new_password: new_password
		  }, function(res){
	  		if(res.code == 0){
				alert("修改密码成功");
			}else{
				alert(res.msg)
			}
	  		$("#passwordModal").modal("hide");
	  		$("#passwordModal input").val("");
		  }).error(function(res){
		  	var res = JSON.parse(res.responseText)
			alert(res.msg)
		  })
	  }
	})
	// 点击生成邀请码
	$("#pro_invitecode").on("click", function(e){
		e.stopPropagation();
		$("#invitecodeModal").modal("show");
	})
	$("#invitecodeModal .modal-sure").on("click", function(e){
		e.stopPropagation();
		var phone = $("#invitecode_phone").val().trim();
		if(phone === ""){
			alert("手机号码不能为空")
		}else if(!checkPhone(phone)){
			alert("手机号码格式不正确")
		}else{
			$.post("/api/auth/invite/",{
				phone: phone
			}, function(res){
				if(res.code === 0){
					var data = res.data;
					$("#invitecode_text").text("（手机号码"+data.phone+"的邀请码为"+data.invite_code+"）");
					$("#invitecodeModal").modal("hide");
					$("#invitecodeModal input").val("");
				}else{
					alert(res.msg)
				}
			}).error(function(res){
				var res = JSON.parse(res.responseText)
				alert(res.msg)
			})
		}
	})
	
})